<?php
error_reporting(1);

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Login extends CI_Controller {

    function index() {
        $data['main_content'] = 'login_form';
        $this->load->view('includes/template', $data);
    }

    function validate_credentials() {
        $data = array();

        $data['password'] = $this->input->post('password');
        $data['emailAddress'] = $this->input->post('username');

        $userID= $this->userID($data);
       
        $vuser = http_build_query(
                array(
                    'password' => $data['password'],
                    'emailAddress' => $data['emailAddress']
              
                )
        );
        $opts = array('http' =>
            array(
                'method' => 'POST',
                'header' => 'Content-type: application/x-www-form-urlencoded',
                'content' => $vuser
            )
        );

        $context = stream_context_create($opts);
        
        $result = json_decode(file_get_contents(base_url() . 'index.php/rest/account/login/format/json',false,$context));
        if($result->obj[0]->sessionKey){
     
            $data = array(
                'username' => $data['emailAddress'],
		'user_id'=>$userID,
                'sessionKey'=>$result->obj[0]->sessionKey,
                'is_logged_in' => true
               
            );
            $this->session->set_userdata($data);
            redirect('site/members_area');
        }
        else
        {
            $data['main_content'] = 'signup_form';
            $data['alertmessage'] = 'You haven\'t been registered with our applicaiton. Please sign up';
           $this->load->view('includes/template', $data);
        }    

    }
    /////////
     function userID($udata) {
       $data = array();
        $data['emailAddress'] = $udata['emailAddress'];
        
        $userID = http_build_query(
                array(
                    'emailAddress' => $data['emailAddress']
                )
        );
        $opts = array('http' =>
            array(
                'method' => 'POST',
                'header' => 'Content-type: application/x-www-form-urlencoded',
                'content' => $userID
            )
        );
        $context = stream_context_create($opts);
        $result = json_decode(file_get_contents(base_url() . 'index.php/rest/account/userID/format/json', false, $context));
        return $result->user_id;
     }

    function signup() {
        $data['main_content'] = 'signup_form';
        $this->load->view('includes/template', $data);
    }

    function create_member() {
        $this->load->library('form_validation');


        $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');


        if ($this->form_validation->run() == FALSE) {
            $this->load->view('signup_form');
        } else {
            $this->load->model('account_model');

            $new_member_insert_data = array(
                'firstName' => $this->input->post('first_name'),
                'lastName' => $this->input->post('last_name'),
                //'emailAddress' => $this->input->post('email_address'),			
                'emailAddress' => $this->input->post('username'),
                'password' => md5($this->input->post('password'))
            );

            $result = $this->account_model->create_user($new_member_insert_data);
            if ($result) {
                $data['main_content'] = 'signup_successful';
                $this->load->view('includes/template', $data);
            } else {
                $this->load->view('signup_form');
            }
        }
    }

    function logout() {

        $data = array();
        $data['sessionKey'] = $this->session->userdata('sessionKey');

        $ldata = http_build_query(
                array(
                    'sessionKey' => $data['sessionKey']
                )
        );

        $opts = array('http' =>
            array(
                'method' => 'POST',
                'header' => 'Content-type: application/x-www-form-urlencoded',
                'content' => $ldata
            )
        );

        $context = stream_context_create($opts);
        $result = file_get_contents(base_url() . 'index.php/rest/account/logout', false, $context);
        $this->session->sess_destroy();
        $data['main_content'] = 'login_form';
        redirect('login', 'refresh');
        $data['val'] = '<script language="javascript" type="text/javascript">
            function logout()
            {
			  FB.logout();
		    }
       </script>';
    }

}

?>
